package com.zem.imos.Util;

import com.zem.imos.Entity.Admin;
import com.zem.imos.Entity.Enum.Role;
import com.zem.imos.Entity.Params.ParamsAdmin;
import com.zem.imos.Entity.Params.ParamsDoctor;
import com.zem.imos.Entity.Params.ParamsPatient;
import com.zem.imos.Entity.Patient;
import com.zem.imos.Entity.User;
import com.zem.imos.Entity.Doctor;
import io.jsonwebtoken.*;

import java.util.Date;

public class JWT {
    private static final String SECRET = "imos";

    public static String createJwtForDoctor(ParamsDoctor paramsDoctor, Long ttlMillis) {
        // 签名算法 HS256，即jjwt已经封装header中需要的算法名称
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        // 生成Jwt的时间，即签发时间
        long nowMillis = System.currentTimeMillis();
        //构建Jwt
        JwtBuilder builder = Jwts.builder()
                //jwt的唯一标识
                .setId(String.valueOf(paramsDoctor.getUsername()))
                //jwt面向用户
                .setSubject(paramsDoctor.getName())
                //jwt的签发时间
                .setIssuedAt(new Date(nowMillis))
                //设置签名使用的签名算法和签名使用的秘钥
                .signWith(signatureAlgorithm,SECRET);
        //自定义payload的claim信息
        builder.claim("role", "医生");
        builder.claim("id", paramsDoctor.getId());
        builder.claim("userId", paramsDoctor.getUserId());
        // 设置过期时间，需要大于签发时间
        if (ttlMillis >= 0) {
            long expMillis = nowMillis + ttlMillis;
            builder.setExpiration(new Date(expMillis));
        }
        String token = builder.compact();
        return token;
    }

    public static String createJwtForAdmin(ParamsAdmin admin, long ttlMillis) {
        // 签名算法 HS256，即jjwt已经封装header中需要的算法名称
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        // 生成Jwt的时间，即签发时间
        long nowMillis = System.currentTimeMillis();
        //构建Jwt
        JwtBuilder builder = Jwts.builder()
                //jwt的唯一标识
                .setId(String.valueOf(admin.getUsername()))
                //jwt面向用户
                .setSubject(admin.getName())
                //jwt的签发时间
                .setIssuedAt(new Date(nowMillis))
                //设置签名使用的签名算法和签名使用的秘钥
                .signWith(signatureAlgorithm,SECRET);
        //自定义payload的claim信息
        builder.claim("role", "管理员");
        builder.claim("id", admin.getId());
        builder.claim("userId", admin.getUserId());
        // 设置过期时间，需要大于签发时间
        if (ttlMillis >= 0) {
            long expMillis = nowMillis + ttlMillis;
            builder.setExpiration(new Date(expMillis));
        }
        String token = builder.compact();
        return token;
    }

    public static String createJwtForPatient(ParamsPatient paramsPatient, long ttlMillis) {
        // 签名算法 HS256，即jjwt已经封装header中需要的算法名称
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        // 生成Jwt的时间，即签发时间
        long nowMillis = System.currentTimeMillis();
        //构建Jwt
        JwtBuilder builder = Jwts.builder()
                //jwt的唯一标识
                .setId(String.valueOf(paramsPatient.getUsername()))
                //jwt面向用户
                .setSubject(paramsPatient.getName())
                //jwt的签发时间
                .setIssuedAt(new Date(nowMillis))
                //设置签名使用的签名算法和签名使用的秘钥
                .signWith(signatureAlgorithm,SECRET);
        //自定义payload的claim信息
        builder.claim("role", "患者");
        builder.claim("id", paramsPatient.getId());
        builder.claim("userId", paramsPatient.getUserId());
        // 设置过期时间，需要大于签发时间
        if (ttlMillis >= 0) {
            long expMillis = nowMillis + ttlMillis;
            builder.setExpiration(new Date(expMillis));
        }
        String token = builder.compact();
        return token;
    }


    public static Claims parseJwt(String jwt){
        //解析jwt
        try {
            JwtParser parser = Jwts.parser();
            //获取解析后的对象
            Claims claims = Jwts.parser()
                    //设置签名秘钥，和生成的签名的秘钥一模一样
                    .setSigningKey(SECRET)
                    //设置需要解析的jwt
                    .parseClaimsJws(jwt)
                    .getBody();
            return claims;
        }
        catch (ExpiredJwtException e){}
        catch (UnsupportedJwtException e) {}
        catch (MalformedJwtException e) {}
        catch (SignatureException e) {}
        catch (IllegalArgumentException e) {}

        return null;

    }


}
